import { ref } from 'vue'
import { adapter, request } from '@/request'
import type { RequestWrapper } from '@/core/interfaces/common'
import { getGuide } from '@/core/api/user'

export interface getGuideType {
  priceAsk: PriceAsk
  orderGuide: OrderGuide[]
}

export interface OrderGuide {
  title: string
  content: Content[]
  orderId: number
}

export interface Content {
  text: string
  url: string
}

export interface PriceAsk {
  title: string
  content: string
  priceAskIdList: number[]
  url: string
}

export default function getGuideFn() {
  const service = adapter<RequestWrapper<getGuideType>>(request)
  const loading = ref(false)
  const response = ref<getGuideType>()

  const fetch = () => {
    const { url, method } = getGuide()
    loading.value = true

    return service({
      config: {
        url,
        method
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
